package com.iot.network;

import com.iot.messaage.SendMessage;
import com.iot.messaage.SendSimpleMqttMessage;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.vertx.core.buffer.Buffer;
import io.vertx.mqtt.MqttEndpoint;

/**
 * @author Administrator
 */
public class MqttDeviceSession implements DeviceSession {

    private final MqttEndpoint exchange;

    public MqttDeviceSession(MqttEndpoint exchange) {
        this.exchange = exchange;

    }

    public MqttEndpoint getExchange() {
        return exchange;
    }

    @Override
    public void onClose(Runnable var1) {

        if (exchange.isConnected()) {
            exchange.close();
        }
    }

    @Override
    public void send(SendMessage reply) {
        SendSimpleMqttMessage sendSimpleMqttMessage = (SendSimpleMqttMessage) reply;
        exchange.publish(sendSimpleMqttMessage.getTopic(), Buffer.buffer(sendSimpleMqttMessage.getReplyStr()), MqttQoS.AT_MOST_ONCE, true, true);
    }
}
